package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.atguigu.gmall.product.mapper.BaseCategory1Mapper;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/6 14:39
 */
@Service
public class BaseAttrInfoServiceImpl
        extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoService {
    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;
    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;
    @Autowired
    private BaseAttrValueService baseAttrValueService;
    @Override
    public List<BaseAttrInfo> getAttrInfoByCategoryId(Long category1Id, Long category2Id, Long category3Id) {
        //lambad：表达式的写法。看需要传递森么样的lambda
        //比如1: void accept();   ()-{}
        //比如2: void accept(T t); (ele)->{}
        //比如3: void accept(T t,X x); (ele,w)->{}
        //比如4: X accept(T t); (ele)->{return instanceof X}
        //比如5: X abc();  ()->{return instanceof X}
        QueryWrapper<BaseAttrInfo> wrapper = new QueryWrapper<>();
        if(!category1Id.equals(0)){
            wrapper.or((wra)->{
                wra.eq("category_id",category1Id);
                wra.eq("category_level",1);
            });

        }
        if(!category2Id.equals(0)){
            wrapper.or((wra)->{
                wra.eq("category_id",category2Id);
                wra.eq("category_level",2);
            });

        }
        if(!category3Id.equals(0)){
            wrapper.or((wra)->{
                wra.eq("category_id",category3Id);
                wra.eq("category_level",3);
            });
        }

        List<BaseAttrInfo> list = baseAttrInfoMapper.selectList(wrapper);

        for (BaseAttrInfo baseAttrInfo : list) {
            QueryWrapper<BaseAttrValue> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("attr_id",baseAttrInfo.getId());
            baseAttrInfo.setAttrValueList(baseAttrValueMapper.selectList(wrapper1));
        }

        return list;
    }
    @Transactional
    @Override
    public void saveAttrInfo(BaseAttrInfo attrInfo) {
        if(attrInfo.getId() == null){
            //新增功能
            insertBaseAttrInfo(attrInfo);
        }else{
            //修改功能
            updateBaseAttrInfo(attrInfo);
        }

    }
    //根据指定id，修改平台属性名及其所属的属性值
    private void updateBaseAttrInfo(BaseAttrInfo attrInfo) {
        //1、按主键更新BaseAttrInfo的名字
        baseAttrInfoMapper.updateById(attrInfo);
        //2、处理集合
        //需要修改的集合
        List<BaseAttrValue> updateList = attrInfo.getAttrValueList().stream().filter((ele) -> {
            return ele.getId() != null;
        }).collect(Collectors.toList());

        //前端提交来的以前存在的数据
        List<Long> noDeleteIds = updateList.stream().map((ele) -> {
            return ele.getId();
        }).collect(Collectors.toList());
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",attrInfo.getId()).notIn("id",noDeleteIds);
        baseAttrValueMapper.delete(wrapper);

        //需要新增的集合
        List<BaseAttrValue> insertList = attrInfo.getAttrValueList().stream().filter((ele) -> {
            return ele.getId() == null;
        }).map((ele) -> {
            ele.setAttrId(attrInfo.getId());
            return ele;
        }).collect(Collectors.toList());

        baseAttrValueService.saveBatch(insertList);
        baseAttrValueService.updateBatchById(updateList);


    }

    //添加平台属性名和其所属的平台属性值
    public void insertBaseAttrInfo(BaseAttrInfo attrInfo){
        //1.先保存平台属性名
        baseAttrInfoMapper.insert(attrInfo);
        //获取自增的id
        Long attrInfoId = attrInfo.getId();
        //2.根据保存的平台属性名的自增id,去保存平台属性名对应的平台属性值
//        for (BaseAttrValue value : attrInfo.getAttrValueList()) {
//            value.setAttrId(attrInfoId);
//            baseAttrValueMapper.insert(value);
//        }
        //使用批量保存，此时的valueList中每一个元素的attrId属性都为null
        List<BaseAttrValue> valueList = attrInfo.getAttrValueList();
        //List集合中每一个元素的attrId属性都有值
        List<BaseAttrValue> collect = valueList.stream().map((ele) -> {
            ele.setAttrId(attrInfoId);
            return ele;
        }).collect(Collectors.toList());
        baseAttrValueService.saveBatch(collect);
    }
}
